package cn.school.mybatis.test;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import cn.school.mybatis.dao.GradeDao;
import cn.school.mybatis.dao.MyBatisUtil;
import cn.school.mybatis.dao.ResultDao;
import cn.school.mybatis.pojo.Grade;
import cn.school.mybatis.pojo.Result;

public class TestGrade {

	// 完成查询年级（grade）信息的 同时获得年级下的课程（subject）
	public void getSubject(int num) {
		SqlSession session = MyBatisUtil.getSqlSession();
		GradeDao gradeDao = session.getMapper(GradeDao.class);
		Grade result = gradeDao.get(num);
		System.out.println(result.getSubject());
		MyBatisUtil.close(session);
	}

	// 查询全部
	public void searchAll() {
		SqlSession session = MyBatisUtil.getSqlSession();
		GradeDao GradeDao = session.getMapper(GradeDao.class);
		List<Grade> list = GradeDao.searchAll();
		MyBatisUtil.close(session);
		for (Grade g : list) {
			System.out.print(g);
		}

	}

	// 查询数量
	public void searchCount() {
		SqlSession session = MyBatisUtil.getSqlSession();
		GradeDao gradeDao = session.getMapper(GradeDao.class);
		int count = gradeDao.searchCount();
		MyBatisUtil.close(session);
		System.out.println("共有" + count + "条记录");
	}

	// 查询单个
	public void getOne(int num) {
		SqlSession session = MyBatisUtil.getSqlSession();
		GradeDao gradeDao = session.getMapper(GradeDao.class);
		Grade result = gradeDao.get(num);
		System.out.println(result);
		MyBatisUtil.close(session);
	}

	// 分页查询
	public void searchLimit(int pageNo, int pageSize) {
		SqlSession session = MyBatisUtil.getSqlSession();
		GradeDao gradeDao = session.getMapper(GradeDao.class);
		int offset = (pageNo - 1) * pageSize;
		offset = Math.max(offset, 0);
		List<Grade> list = gradeDao.searchLimit(offset, pageSize);
		MyBatisUtil.close(session);
		for (Grade s : list) {
			System.out.print(s);
		}
	}

	// 修改
	public void updata(int num, String gname) {
		SqlSession session = MyBatisUtil.getSqlSession();
		GradeDao GradeDao = session.getMapper(GradeDao.class);
		Grade grade = new Grade(num, gname);
		int result = GradeDao.update(grade);
		session.commit();
		System.out.println(result + "条修改成功！");
		MyBatisUtil.close(session);
	}

	// 删除
	public void del(int num) {
		SqlSession session = MyBatisUtil.getSqlSession();
		GradeDao GradeDao = session.getMapper(GradeDao.class);
		int result = GradeDao.del(num);
		session.commit();
		System.out.println(result + "条删除成功！");
		MyBatisUtil.close(session);

	}

	// 增加
	public void add(String gname) {
		SqlSession session = MyBatisUtil.getSqlSession();
		GradeDao GradeDao = session.getMapper(GradeDao.class);
		Grade grade = new Grade(gname);
		int result = GradeDao.add(grade);
		session.commit();
		System.out.println(result + "条增加成功！");
		MyBatisUtil.close(session);

	}

	public static void main(String[] args) {
		TestGrade tg = new TestGrade();
		// 查询全部
		// tg.searchAll();

		// 查询单个
		// tg.getOne(10);

		// 查询数量
		// tg.searchCount();

		// 修改
		// tg.updata(20, "mybatis-1");

		// 删除
		// tg.del(20);

		// 增加
		// tg.add("mybatis-3");

		// 分页查询
		tg.searchLimit(2, 5);

		// 完成查询年级（grade）信息的 同时获得年级下的课程（subject）
		// tg.getSubject(2);
		// 这个不对……

	}

}
